<template>
    <div id='tmpl'>
        <!-- 实现提交评论到服务器的静态结构 -->
        <div id="postcomment">
            <h3>提交评论</h3>
            <p class="p"></p>
            <textarea placeholder="请输入您要评论的内容..." v-model="postcontent"></textarea>
            <mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
        </div>
        <!-- 获取评论数据列表 -->
        <div id="list">
            <h3>评论列表</h3>
            <p class="p"></p>
            <div v-for="(item, index) in list" :key="index">
                <div class="title">
                    <span>第{{index+1}}楼</span>
                    <span>用户：{{item.user_name}}</span>
                    <span>发表时间：{{item.add_time | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell" v-text="item.content"></li>
                </ul>
            </div>
        </div>
        <!-- 获取更多按钮 -->
        <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
    </div>
</template>
<script>
import { Toast } from 'mint-ui';
export default {
    props: ['id'],
    data() {
        return {
            postcontent: '',
            list: [],
            pageindex: 1
        }
    }, //
    created() {
        this.getcomment(this.pageindex);
    },
    methods: {
        //   评论数据的提交
        postcomment() {
            // 判断用户是否填写了评论
            if (this.postcontent.trim().length <= 0) {
                Toast('评论内容不能为空');
                this.postcontent = '';
                return;
            }
            var url = this.$common.apidomain + '/api/postcomment/' + this.id;
            this.$http.post(url, { content: this.postcontent }, { emulateJSON: true }).then(function (res) {
                Toast(res.body.message);

                //将最新评论追加到评论列表最顶部
                this.list = [{
                    "user_name": "匿名用户",
                    "add_time": new Date(),
                    "content": this.postcontent
                }].concat(this.list);

                // this.list.unshift({
                //     "user_name": "匿名用户",
                //     "add_time": new Date(),
                //     "content": this.postcontent
                // });

                //清空输入框内容
                this.postcontent = '';
            });
        },
        //获取当前评论数据
        getcomment(pageindex) {
            pageindex = pageindex || 1;//防止用户忘记传数据
            var url = this.$common.apidomain + '/api/getcomments/' + this.id + '?pageindex=' + pageindex;
            this.$http.get(url).then(function (res) {
                if (res.body.status != 0) {
                    Toast(res.body.message);
                    return;
                }
                // this.list = this.list.concat(res.body.message);
                this.list.push.apply(this.list, res.body.message);
            });
        },
        //评论加载更多功能
        getmore() {
            this.pageindex++;
            //获取当前页面的评论数据
            this.getcomment(this.pageindex);
        }
    }
}
</script>
<style lang="css" scoped>
#postcomment,
#list {
    padding: 5px;
}

.p {
    height: 1px;
    width: 100%;
    border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}

.title {
    padding: 5px;
    color: #6d6d72;
    font-size: 15px;
    background-color: rgba(0, 0, 0, 0.1)
}
</style>